package com.tanhua.es.controller;

import com.tanhua.es.pojo.UserLocationES;
import com.tanhua.es.service.UserLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/28 21:39
 * @version: 0.0.1
 * @modified By:
 */
@RestController
@RequestMapping("es/user/location")
public class UserLocationController {

    @Autowired
    private UserLocationService userLocationService;
    /**
     * @description 更新用户地理位置
     * @author
     * @date
     * @param
     *@return
     */
    @PostMapping
    public ResponseEntity<UserLocationES>updateUserLocation(@RequestBody Map<String,Object>param){
        try {
            Long userId=Long.valueOf(param.get("userId").toString());
            Double longitude=Double.valueOf(param.get("longitude").toString());
            Double latitude=Double.valueOf(param.get("latitude").toString());
            String address=param.get("address").toString();
            Boolean bool= this.userLocationService.updateUserLocation(userId,longitude,latitude,address);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 查询用户的地理位置
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("{userId}")
    public ResponseEntity<UserLocationES>queryUserLocation(@PathVariable("userId")Long userId){
        try {
            UserLocationES userLocationES= this.userLocationService.queryUserById(userId);
            if(null==userLocationES){
                return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
            }
            return ResponseEntity.ok(userLocationES);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 根据位置搜索
     * @author
     * @date
     * @param
     *@return
     */
    @PostMapping("list")
    public ResponseEntity<List<UserLocationES>>queryUserFromLocation(@RequestBody Map<String,Object>param){
        try {
            Double longitude=Double.valueOf(param.get("longitude").toString());
            Double latitude=Double.valueOf(param.get("latitude").toString());
            Double distance=Double.valueOf(param.get("distance").toString());
            Integer page=param.get("page")==null?1:Integer.valueOf(param.get("page").toString());
            Integer pageSize=param.get("pageSize")==null?100:Integer.valueOf(param.get("pageSize").toString());
            Page<UserLocationES>userLocationES=this.userLocationService.queryUserFromLocation(longitude,latitude,distance,page,pageSize);
            return ResponseEntity.ok(userLocationES.getContent());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
